package cn.com.generaldata.os.dao.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;

import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.dao.intf.BaseDAOInterface;
import cn.com.generaldata.os.newpojo.IndicatorAccounting;
import cn.com.generaldata.os.newpojo.IndicatorInput;
import cn.com.generaldata.os.newpojo.SystemCodedic;
import cn.com.generaldata.os.paging.Event;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.paging.Page;
import cn.com.generaldata.os.paging.Result;
import cn.com.generaldata.os.pojo.Area;
import cn.com.generaldata.os.pojo.IndicatorAccount;
import cn.com.generaldata.os.pojo.IndicatorDataView;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorDataVO;

public class IndicatorDAO extends BaseDAO<IndicatorDataView> {
	private BaseDAOInterface<IndicatorInput> indicatorInputDAO;

	private BaseDAOInterface<IndicatorAccounting> indicatorAccountingDAO;
	private BaseDAOInterface<SystemCodedic> sysCodedicDAO;

	public void setSysCodedicDAO(BaseDAOInterface<SystemCodedic> sysCodedicDAO) {
		this.sysCodedicDAO = sysCodedicDAO;
	}

	private BaseDAOInterface<IndicatorDataView> indicatorDAO;

	public void setIndicatorAccountingDAO(BaseDAOInterface<IndicatorAccounting> indicatorAccountingDAO) {
		this.indicatorAccountingDAO = indicatorAccountingDAO;
	}

	public void setIndicatorInputDAO(BaseDAOInterface<IndicatorInput> indicatorInputDAO) {
		this.indicatorInputDAO = indicatorInputDAO;
	}

	public void setIndicatorDAO(BaseDAOInterface<IndicatorDataView> indicatorDAO) {
		this.indicatorDAO = indicatorDAO;
	}

	public void setAreaDAO(BaseDAOInterface<Area> areaDAO) {
		this.areaDAO = areaDAO;
	}

	private BaseDAOInterface<IndicatorAccount> indicatorAccountDAO;

	private BaseDAOInterface<Area> areaDAO;

	public void setIndicatorAccountDAO(BaseDAOInterface<IndicatorAccount> indicatorAccountDAO) {
		this.indicatorAccountDAO = indicatorAccountDAO;
	}

	// 取得指标分类
	public List<SystemCodedic> queryType() {
		Filter sysFilter = new Filter();
		sysFilter.addRule(new FilterRule(FilterConstant.EQUAL, "section", "10028"));
		List<SystemCodedic> list = sysCodedicDAO.find(SystemCodedic.class, sysFilter, null);
		return list;
	}

	public Result queryIndicatorData(IndicatorDataVO indiVo, Page page) {
		List<SystemCodedic> types = queryType();
		String hql = "from IndicatorDataView where 1=1";
		HashMap map = new HashMap();
		if (indiVo != null) {
			if (!StringUtils.isBlank(indiVo.getIndicatorName())) {
				hql += " and name like:indicatorName";
				map.put("indicatorName", "%" + indiVo.getIndicatorName() + "%");
			}
			if (indiVo.getAreaId() != null) {
				if ("320000000000000".equals(indiVo.getAreaId())) {
					hql += " and (areaId=:areaId or areaId is null or areaId='')";
				} else {
					hql += " and areaId=:areaId";
				}
				map.put("areaId", indiVo.getAreaId());
			}
			// filter.addRule(new FilterRule(FilterConstant.EQUAL, "areaId", indiVo.getAreaId()));
			if (indiVo.getTimeType() != null && !"".equals(indiVo.getTimeType().toString())) {
				// 半年报
				if (!"3".equals(indiVo.getTimeType().toString())) {
					hql += " and timeType=:timeType";
					map.put("timeType", indiVo.getTimeType());
				}
				// filter.addRule(new FilterRule(FilterConstant.EQUAL, "timeType",
				// indiVo.getTimeType()));
				else {
					hql += " and yearTime=:yearTime";
					map.put("yearTime", indiVo.getYearTime());
				}
				// filter.addRule(new FilterRule(FilterConstant.EQUAL, "yearTime",indiVo.getYearTime()));
			}
			if (indiVo.getYear() != null && indiVo.getYear() != 0) {
				hql += " and year=:year";
				map.put("year", indiVo.getYear());
			}
			// filter.addRule(new FilterRule(FilterConstant.EQUAL, "year",
			// indiVo.getYear()));
			if (indiVo.getMonth() != null && indiVo.getMonth() != 0) {
				hql += " and month=:month";
				map.put("month", indiVo.getMonth());
			}
			// filter.addRule(new FilterRule(FilterConstant.EQUAL, "month",
			// indiVo.getMonth()));
			if (indiVo.getQuarter() != null && indiVo.getQuarter() != 0) {
				hql += " and quarter=:quarter";
				map.put("quarter", indiVo.getQuarter());
			}
			if (indiVo.getSection() != null && !"".equals(indiVo.getSection())) {
				hql += " and category=:section";
				map.put("section", Integer.valueOf(indiVo.getSection()));
			}
			if (indiVo.getIndustryCode() != null && !"".equals(indiVo.getIndustryCode())) {
				hql += " and industryCode=:industryCode";
				map.put("industryCode", indiVo.getIndustryCode());
			}
			// filter.addRule(new FilterRule(FilterConstant.EQUAL, "quarter",
			// indiVo.getQuarter()));
		}
		hql += " order by year desc";
		Query query = getSession().createQuery(hql);
		if (map != null) {
			query.setProperties(map);
		}
		List<IndicatorDataView> maxlist = query.list();
		int size = maxlist.size();
		if (page == null) {
			query.setFirstResult(0);
			query.setMaxResults(20);
		} else {
			query.setFirstResult(page.getFirstResult());
			query.setMaxResults(page.getPageSize());

		}
		List<IndicatorDataView> findByHql = query.list();
		List<Integer> nullId = new ArrayList<Integer>();
		List<IndicatorDataVO> list = new ArrayList<IndicatorDataVO>();
		if (findByHql != null && findByHql.size() > 0) {
			for (IndicatorDataView input : findByHql) {
				// IndicatorInput indicatorInput = indicatorInputDAO.get(IndicatorInput.class, input.getIndicatorId());
				// if(indicatorInput==null)
				// {
				// size--;
				// nullId.add( input.getIndicatorId());
				// }
				// else
				// {
				//
				IndicatorDataVO inputVO = BeanMapper.map(input, IndicatorDataVO.class);
				inputVO.setIndicatorName(input.getName());
				inputVO.setIndicatorUnit(input.getUnit());
				inputVO.setSection(input.getCategory().toString());
				for (SystemCodedic type : types) {
					if (type.getCode().equals(input.getCategory().toString())) {
						inputVO.setSectionName(type.getCodeName());
						break;
					}
				}
				DecimalFormat nf = new DecimalFormat();
				nf.setMinimumFractionDigits(2);
				nf.setMaximumFractionDigits(2);
				inputVO.setShowValue(nf.format(inputVO.getIndicatorValue()));
				list.add(inputVO);
				// }
			}
			if (page == null) {
				return new Result(1, 1, size, list, new ArrayList<Event>());
			} else {
				return new Result(page.getPageNumber(), page.getPageSize(), size, list, new ArrayList<Event>());
				// int count = indicatorDAO.count(IndicatorData.class, filter);
				// List<IndicatorData> find = null;
				// if (page == null) {
				// find = indicatorDAO.find(IndicatorData.class, filter, null);
				// if (find != null && find.size() > 0) {
				// List<IndicatorDataVO> list = new ArrayList<IndicatorDataVO>();
				// for (IndicatorData input : find) {
				// IndicatorInput indicatorInput = indicatorInputDAO.get(IndicatorInput.class, input.getIndicatorId());
				// IndicatorDataVO inputVO = BeanMapper.map(input,
				// IndicatorDataVO.class);
				// inputVO.setIndicatorName(indicatorInput.getName());
				// inputVO.setIndicatorUnit(indicatorInput.getUnit());
				// list.add(inputVO);
				// }
				//
				// return new Result(1, 1, count, list,
				// new ArrayList<Event>());
				// }
				// } else {
				// find = indicatorDAO.find(IndicatorData.class, filter, page);
				// if (find != null && find.size() > 0) {
				// List<IndicatorDataVO> list = new ArrayList<IndicatorDataVO>();
				// for (IndicatorData input : find) {
				// IndicatorInput indicatorInput = indicatorInputDAO.get(IndicatorInput.class, input.getIndicatorId());
				// IndicatorDataVO inputVO = BeanMapper.map(input,
				// IndicatorDataVO.class);
				// inputVO.setIndicatorName(indicatorInput.getName());
				// inputVO.setIndicatorUnit(indicatorInput.getUnit());
				// list.add(inputVO);
				// }
				// return new Result(page.getPageNumber(), page.getPageSize(),
				// count, list, new ArrayList<Event>());
				// }
			}
		}
		for (Integer i : nullId) {
			System.out.println("空的指标ID为: " + i);
		}
		return null;
	}
}
